package com.tadpole.music.view.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tadpole.music.R;
import com.youth.banner.loader.ImageLoader;
import com.zhq.utils.wordtohtml.ScreenUtils;


/**
 * 创建人：zhouliqiang
 * 日  期：2019/1/8  4:04 PM
 * 类说明：GlideImageLoader
 */
public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(final Context context, Object path, final ImageView imageView) {
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.banner_image)
                .error(R.mipmap.banner_image)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        if(!path.equals("")) {
            //Glide 加载图片简单用法
            Glide.with(context).load(path).apply(requestOptions).into(imageView);
        }else{
//            Glide.with(context).load(R.mipmap.loading).apply(requestOptions).into(imageView);

            Glide.with(context).asBitmap().load(path).into((new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    int imageWidth = resource.getWidth();
                    int imageHeight = resource.getHeight();
                    int height = ScreenUtils.getScreenWidth(context) * imageHeight / imageWidth;
                    ViewGroup.LayoutParams para = imageView.getLayoutParams();
                    para.height = height;
                    para.width = ScreenUtils.getScreenWidth(context);
                    imageView.setImageBitmap(resource);
                }
            })
            );
        }
    }
}